﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using S4Store.Service;
using S4Store.Models.FinancialKpi;
using S4Store.Domain.FinancialKpi;
using S4Store.Domain;

namespace S4Store.Controllers
{
    public class FinancialKpiController : ReportBaseController
    {
        public FinancialKpiService FinancialKpiService { get; set; }
        public EmployeeService SellerService { get; set; }
        public CarService CarService { get; set; }

        public ActionResult Index()
        {
            IndexModel model = new IndexModel();
            InjectReportModel(model);
            using (var t = new Transtion())
            {
                t.Inject(FinancialKpiService);
                var data = FinancialKpiService.GetFinancialDayDataQuery().Where(x => x.StoreId == Store.Id).ToList();
                model.MonthDayData = data;
                return View(model);
            }
        }

        public ActionResult MonthTd(int y,int m)
        {
            using (var t = new Transtion())
            {
                t.Inject(FinancialKpiService);
                var config = FinancialKpiService.GetFinancialMonthConfig(y, m, Store.Id);
               
                return View(config);
            }
            
        }

        public ActionResult InputFinancialKpiMonthConfig(int y, int m)
        {
            using (var t = new Transtion())
            {
                t.Inject(FinancialKpiService);
                var config = FinancialKpiService.GetFinancialMonthConfig(y, m, Store.Id);
                
                return View(config);
            }
        }

        public JsonResult ModifyBackInputFinancialKpiMonthConfig(FinancialMonthConfig config, List<FinancialCarBrandMonthConfig> brandconfig, bool isconfirm = false)
        {
            using (var t = new Transtion())
            {
                t.Inject(FinancialKpiService);
                var dbconfig = FinancialKpiService.GetFinancialMonthConfigQuery().FirstOrDefault(x => x.Id == config.Id);
                if (dbconfig == null)
                {
                    dbconfig = new FinancialMonthConfig();
                }
                if (isconfirm)
                {
                    dbconfig.IsConfirmed = true;
                }
                dbconfig.StoreId = Store.Id;
                dbconfig.Month = config.Month;
                dbconfig.GSFWXSLVMB = config.GSFWXSLVMB;
                dbconfig.MBZZTS = config.MBZZTS;
                dbconfig.YYCB = config.YYCB;
                dbconfig.MBSHZCZ = config.MBSHZCZ;
                dbconfig.SPCYYEBL = config.SPCYYEBL;
                dbconfig.ZHYYEBL = config.ZHYYEBL;
                dbconfig.BXYYEBL = config.BXYYEBL;
                dbconfig.DKFWFBL = config.DKFWFBL;
                dbconfig.ESCMLBL = config.ESCMLBL;
                dbconfig.YBXSEZB = config.YBXSEZB;
                dbconfig.SGCCZBL = config.SGCCZBL;
                dbconfig.BYCZZB = config.BYCZZB;
                dbconfig.WXCZZB = config.WXCZZB;
                dbconfig.BJCZBL = config.BJCZBL;
                dbconfig.ZHCZZB = config.ZHCZZB;
                dbconfig.YHPCZZB = config.YHPCZZB;
                FinancialKpiService.SubmitFinancialKpiMonthConfig(dbconfig);

                if (brandconfig == null)
                {
                    brandconfig = new List<FinancialCarBrandMonthConfig>();
                }
                var list = new List<FinancialCarBrandMonthConfig>();
                foreach (var sel in brandconfig)
                {
                    sel.Month = config.Month;
                    sel.StoreId = Store.Id;
                    if (isconfirm)
                    {
                        sel.IsConfirmed = true;
                    }
                    if (sel.Enble)
                    {
                        list.Add(sel);
                    }
                }
                FinancialKpiService.SubmitFinancialCarBrandMonthConfig(config.Month.Year, config.Month.Month, Store.Id, list);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success = true
                    }
                };
            }
        }

        public ActionResult CarBrandList(int y, int m)
        {
            using (var t = new Transtion())
            {
                t.Inject(CarService);
                t.Inject(FinancialKpiService);
                CarBrandListModel model = new CarBrandListModel();
                var brands = CarService.GetEnableCarBrand(Store.PinPaiId);
                model.CarBrands = brands.ToList();
                model.CarBrandMonthConfigs = FinancialKpiService.GetFinancialCarBrandMonthConfig(y, m, Store.Id).ToList();
                return View(model);
            }
        }

        public ActionResult InputFinancialKpiDayData(int y, int m, int d)
        {
            InputFinancialKpiDayDataModel model = new InputFinancialKpiDayDataModel();
            using (var t = new Transtion())
            {
                t.Inject(FinancialKpiService);
                t.Inject(CarService);
                //var brands = CarService.GetEnableCarBrand().ToList();
                model.CarBrands = FinancialKpiService.GetCarbrandsForStoreMonth(y, m, Store.Id).ToList();
                model.Date = new DateTime(y, m, d);
                model.DayDatas = FinancialKpiService.GetFinancialDayDatas(y, m, d, Store.Id).ToList();
                model.MonthConfigs = FinancialKpiService.GetFinancialCarBrandMonthConfig(y, m, Store.Id).ToList();
                model.IsConfirmed = false;
                if (model.DayDatas.Any(x => x.IsConfirmed))
                {
                    model.IsConfirmed = true;
                }
                return View(model);
            }
        }

        public ActionResult ModifyBackInputFinancialKpiDayData(List<FinancialDayData> rows, int y, int m, int d, bool isconfirm = false)
        {
            foreach (var row in rows)
            {
                if (isconfirm)
                {
                    row.IsConfirmed = true;
                }
                else
                {
                    row.IsConfirmed = false;
                }
                row.StoreId = Store.Id;
            }
            using (var t = new Transtion())
            {
                t.Inject(FinancialKpiService);
                FinancialKpiService.SubmitFinancialDayDatas(y, m, d, Store.Id, rows);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success = true
                    }
                };
            }
        }
    }
}
